<template>
    <page-header-wrapper @back="onBack">
        <template v-slot:extra>
            <a-button v-if="data.attachments" @click="download">下载简历</a-button>
            <a-button type="primary" @click="evaluate">评价</a-button>
        </template>
        <process-flows :steps="steps"/>
        <a-tabs default-active-key="1" style="margin-top: 24px">
            <a-tab-pane key="1" tab="简历">
                <resume-content :data="data"></resume-content>
            </a-tab-pane>
            <a-tab-pane key="2" tab="评价">
                <evaluate-list :evaluates="evaluates"/>
            </a-tab-pane>
        </a-tabs>
        <e-form :visible="modal.visible" :loading="modal.loading" :model="modal.model"
                @ok="evaluateOk" @cancel="modal.visible = modal.loading = false"/>
    </page-header-wrapper>
</template>

<script>
    import API from '@/api'
    import moment from 'moment'
    import store from '@/system/store'
    import Adapter from '@/utils/adapter'
    import ProcessFlows from "@/components/ResumeDetails/ProcessFlows";
    import ResumeContent from "@/components/ResumeDetails/ResumeContent";
    import EvaluateList from "@/components/ResumeDetails/EvaluateList";
    import EForm from '@/components/ResumeDetails/EvaluateForm'

    export default {
        name: "Details",
        components: {
            ProcessFlows,
            ResumeContent,
            EvaluateList,
            EForm
        },
        data() {
            return {
                data: {},
                evaluates: [],
                steps: {current: 0, status: 'wait'},
                modal: {
                    visible: false,
                    loading: false,
                    model: {}
                },
                evaluated: false,
                colors: ['red', 'green', 'black']
            }
        },
        mounted() {
            if (this.$route.params.id) {
                API.Resume.Details({id: this.$route.params.id}).then((res) => {
                    if (res) {
                        const data = res.data.resume
                        if (data.positionId && this.$route.params.positionMap)
                            data.positionId = this.$route.params.positionMap[data.positionId]
                        data.ext = data.extJson ? JSON.parse(data.extJson) : null
                        Adapter.ResumeExt(data.ext)
                        if (data.ext && data.ext.study) {
                            for (let i = 0; i < data.ext.study.length; i++) {
                                let v = data.ext.study[i]
                                if (v.time) v.time = moment(v.time[0]).format("YYYY-MM-DD") + '~' +
                                    moment(v.time[1]).format("YYYY-MM-DD")
                            }
                            for (let i = data.ext.study.length; i < 3; i++) {
                                data.ext.study.push({})
                            }
                        }
                        else if (data.ext) data.ext.study = [{}, {}, {}]
                        data.birth = data.birth ? moment(data.birth).format("YYYY-MM-DD") : '-'
                        data.graduationTime = data.graduationTime ? moment(data.graduationTime).format("YYYY-MM-DD") : '-'
                        this.data = data
                        this.steps = Adapter.ResumeState(data)
                        this.steps.members = res.data.members
                        const evaluates = res.data.resumeEvaluates
                        if (evaluates && evaluates.length > 0) {
                            evaluates.forEach((item) => {
                                item.color = this.colors[item.advise]
                                item.time = moment(item.updateTime)
                                if (item.memberId === store.user.id) this.evaluated = item
                            })
                        }
                        this.evaluates = evaluates
                    }
                })
            }
        },
        methods: {
            onBack() {
                this.$router.push({path: this.$route.params.back})
            },
            download() {
                window.open('/' + this.data.attachments, "_blank")
            },
            evaluate() {
                const model = {}
                if (this.evaluated) {
                    model.id = this.evaluated.id
                    model.advise = this.evaluated.advise
                    model.comment = this.evaluated.comment
                }
                else {
                    model.resumeId = this.data.id
                    model.advise = 1
                    model.comment = ''
                }
                this.modal.model = model
                this.modal.visible = true
            },
            evaluateOk(params) {
                this.modal.loading = true
                if (params.id) {
                    API.Evaluate.Edit(params).then((data) => this.processor(data, params))
                        .finally(() => this.modal.loading = false)
                }
                else {
                    API.Evaluate.Add(params).then((data) => this.processor(data, params))
                        .finally(() => this.modal.loading = false)
                }
            },
            processor(res, params) {
                if (res) {
                    const user = store.user
                    this.modal.visible = false
                    params.memberRole = user.title + ' ' + user.name
                    params.time = moment()
                    params.color = this.colors[params.advise]
                    if (params.id) {
                        this.evaluates = this.evaluates.filter((item) => item.id !== params.id)
                    }
                    else {
                        params.id = res.data
                    }
                    this.evaluated = params
                    this.evaluates.push(params)
                    this.$notification.success({message: '提示', description: '评价成功'})
                }
            }
        }
    }
</script>
